package com.group525.cloudef.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group525.cloudef.common.CommonResult;
import com.group525.cloudef.entity.user.UImage;
import com.group525.cloudef.entity.user.UReport;
import com.group525.cloudef.mapper.user.UImageMapper;
import com.group525.cloudef.mapper.user.UReportMapper;
import com.group525.cloudef.service.user.UReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author group525
 * @since 2023-05-23
 */
@Service
public class UReportServiceImpl extends ServiceImpl<UReportMapper, UReport> implements UReportService {


    @Autowired
    UReportMapper uReportMapper;

    @Resource
    UImageMapper ImageMapper;

    @Override
    public CommonResult<?> getMyReports(String id, String time) {
        Optional<String> idOpt = Optional.ofNullable(id);
        if (idOpt.isPresent()) {
            QueryWrapper<UReport> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", id); // 默认查询条件
            switch (time) {
                case "全部时间":
                    // 不添加其他条件即可查询全部时间的数据
                    break;
                case "今天":
                    // 查询当天的数据，条件为 update_time 在 当天 0 点到明天 0 点之间
                    LocalDateTime todayStart = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
                    LocalDateTime todayEnd = todayStart.plusDays(1);
                    queryWrapper.between("update_time", todayStart, todayEnd);
                    break;
                case "近一周":
                    // 查询最近一周的数据，条件为 update_time 在 7 天前到现在之间
                    LocalDateTime weekStart = LocalDateTime.now().minusWeeks(1);
                    LocalDateTime weekEnd = LocalDateTime.now();
                    queryWrapper.between("update_time", weekStart, weekEnd);
                    break;
                case "近一月":
                    // 查询最近一个月的数据，条件为 update_time 在 30 天前到现在之间
                    LocalDateTime monthStart = LocalDateTime.now().minusMonths(1);
                    LocalDateTime monthEnd = LocalDateTime.now();
                    queryWrapper.between("update_time", monthStart, monthEnd);
                    break;
                case "近一年":
                    // 查询最近一年的数据，条件为 update_time 在 365 天前到现在之间
                    LocalDateTime yearStart = LocalDateTime.now().minusYears(1);
                    LocalDateTime yearEnd = LocalDateTime.now();
                    queryWrapper.between("update_time", yearStart, yearEnd);
                    break;
            }

            List<UReport> uReports = uReportMapper.selectList(queryWrapper);
            return CommonResult.success(null, uReports);
        }
        return null;
    }

    @Override
    public CommonResult<?> getImageByReportNo(String id) {
        Optional<String> idOpt = Optional.ofNullable(id);
        if (idOpt.isPresent()) {
            QueryWrapper<UImage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("report_no", id);
            UImage uImage = ImageMapper.selectOne(queryWrapper);
            if (Optional.ofNullable(uImage).isPresent()) {
                return CommonResult.success(null, uImage.getImage());
            }
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }


}
